<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Think\Controller;

class UserController extends AdminController {

    /**
     * 用户管理首页
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function index(){
        $nickname       =   I('name');
        $map['status']  =   array('egt',0);
        if(is_numeric($nickname)){
            $map['uid|nickname']=   array(intval($nickname),array('like','%'.$nickname.'%'),'_multi'=>true);
        }else{
            $map['nickname']    =   array('like', '%'.(string)$nickname.'%');
        }

        $list   = $this->lists('Member', $map);
        //dump($list);
        $this->assign('_list', $list);
        $this->meta_title = '用户信息';
        $this->display();
    }

    /**
     * 修改密码初始化
     * @author huajie <banhuajie@163.com>
     */
    public function edit($id = 0,$name = '', $password = '', $rpassword = '', $email = ''){
	
		if(empty($id)){
			$this->error('请选择用户ID');
		}
		
        if(IS_POST){
            /* 检测密码 */
            if(!empty($password) && $password != $rpassword){
                $this->error('密码存在必须一致！');
            }

            
                $user = array('email' => $email, 'password' =>md5($password), 'nickname' => $username, 'status' => 1 ,
					'reg_time' => time(),'reg_ip'=>ip2long(get_client_ip()),
					
				);
                if(!M('User')->add($user)){
                    $this->error('用户添加失败！');
                } else {
                    $this->success('用户添加成功！',U('Admin/Index/index'));
                }
        } else {
			$User = M("User"); 
			$data = $User->where(array('id'=>$id,'status' => 1))->find();
			//dump($data);
            //$this->meta_title = '新增用户';
			
			$this->assign('data', $data);
            $this->display();
        }
    }

    /**
     * 修改密码提交
     * @author huajie <banhuajie@163.com>
     */
    public function edit_password(){
        //获取参数
        $password   =   I('post.old');
        empty($password) && $this->error('请输入原密码');
        $data['password'] = I('post.password');
        empty($data['password']) && $this->error('请输入新密码');
        $repassword = I('post.repassword');
        empty($repassword) && $this->error('请输入确认密码');

        if($data['password'] !== $repassword){
            $this->error('您输入的新密码与确认密码不一致');
        }

        $Api    =   new UserApi();
        $res    =   $Api->updateInfo(UID, $password, $data);
        if($res['status']){
            $this->success('修改密码成功！');
        }else{
            $this->error($res['info']);
        }
    }

    /**
     * 会员状态修改
     * @author 朱亚杰 <zhuyajie@topthink.net>
     */
    public function changeStatus($method=null){
        $id = array_unique((array)I('id',0));
        if( in_array(C('USER_ADMINISTRATOR'), $id)){
            $this->error("不允许对超级管理员执行该操作!");
        }
        $id = is_array($id) ? implode(',',$id) : $id;
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        $map['uid'] =   array('in',$id);
        switch ( strtolower($method) ){
            case 'forbiduser':
                $this->forbid('Member', $map );
                break;
            case 'resumeuser':
                $this->resume('Member', $map );
                break;
            case 'deleteuser':
                $this->delete('Member', $map );
                break;
            default:
                $this->error('参数非法');
        }
    }

    public function add($username = '', $password = '', $rpassword = '', $email = ''){
        if(IS_POST){	
                //$user = array('email' => $email, 'password' => $password,$rpassword => 'rpassword', 'username' => $username);
                
				$User = D("User"); // 实例化User对象
				$data = getData(); // 通过getData方法获取数据源的（数组）数据
				
				if (!$User->create()){     // 如果创建失败 表示验证没有通过 输出错误提示信息     
					$this->error($User->getError());
				}else{     // 验证通过 可以进行其他数据操作
					if($User->add()){
						$this->success('用户添加成功！',U('Admin/User/index'));
					} else {
						$this->error('新增失败');
					}
				}
        } else {
            $this->display();
        }
    }

}
